﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrinterMonitorSvc.Ext
{
    /// <summary>
    /// DateTime扩展方法
    /// </summary>
    public static class DateTimeExt
    {
        #region 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
        /// <summary>
        /// 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <param name="isRemoveSecond">是否移除秒</param>
        public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
        {
            return dateTime.ToString(isRemoveSecond ? "yyyy-MM-dd HH:mm" : "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
        /// <summary>
        /// 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <param name="isRemoveSecond">是否移除秒</param>
        public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
        {
            return dateTime == null ? string.Empty : ToDateTimeString(dateTime.Value, isRemoveSecond);
        }
        #endregion

        #region 获取格式化字符串，不带时分秒，格式："yyyy-MM-dd"
        /// <summary>
        /// 获取格式化字符串，不带时分秒，格式："yyyy-MM-dd"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToDateString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }
        #endregion

        #region 获取格式化字符串，不带时分秒，格式："yyyy-MM-dd"
        /// <summary>
        /// 获取格式化字符串，不带时分秒，格式："yyyy-MM-dd"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToDateString(this DateTime? dateTime)
        {
            return dateTime == null ? string.Empty : ToDateString(dateTime.Value);
        }
        #endregion

        #region 获取格式化字符串，不带年月日，格式："HH:mm:ss"
        /// <summary>
        /// 获取格式化字符串，不带年月日，格式："HH:mm:ss"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToTimeString(this DateTime dateTime)
        {
            return dateTime.ToString("HH:mm:ss");
        }
        #endregion

        #region 获取格式化字符串，不带年月日，格式："HH:mm:ss"
        /// <summary>
        /// 获取格式化字符串，不带年月日，格式："HH:mm:ss"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToTimeString(this DateTime? dateTime)
        {
            return dateTime == null ? string.Empty : ToTimeString(dateTime.Value);
        }
        #endregion

        #region 获取格式化字符串，带毫秒，格式："yyyy-MM-dd HH:mm:ss.fff"
        /// <summary>
        /// 获取格式化字符串，带毫秒，格式："yyyy-MM-dd HH:mm:ss.fff"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToMillisecondString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
        #endregion

        #region 获取格式化字符串，带毫秒，格式："yyyy-MM-dd HH:mm:ss.fff"
        /// <summary>
        /// 获取格式化字符串，带毫秒，格式："yyyy-MM-dd HH:mm:ss.fff"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToMillisecondString(this DateTime? dateTime)
        {
            return dateTime == null ? string.Empty : ToMillisecondString(dateTime.Value);
        }
        #endregion

        #region 获取格式化字符串，不带时分秒，格式："yyyy年MM月dd日"
        /// <summary>
        /// 获取格式化字符串，不带时分秒，格式："yyyy年MM月dd日"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToChineseDateString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy年MM月dd日");
        }
        #endregion

        #region 获取格式化字符串，不带时分秒，格式："yyyy年MM月dd日"
        /// <summary>
        /// 获取格式化字符串，不带时分秒，格式："yyyy年MM月dd日"
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToChineseDateString(this DateTime? dateTime)
        {
            return dateTime == null ? string.Empty : ToChineseDateString(dateTime.SafeValue());
        }
        #endregion

        #region 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
        /// <summary>
        /// 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <param name="isRemoveSecond">是否移除秒</param>
        public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
        {
            string format = isRemoveSecond ? "yyyy年MM月dd日 HH时mm分" : "yyyy年MM月dd日 HH时mm分ss秒";
            return dateTime.ToString(format);
        }
        #endregion

        #region 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
        /// <summary>
        /// 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <param name="isRemoveSecond">是否移除秒</param>
        public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
        {
            return dateTime == null ? string.Empty : ToChineseDateTimeString(dateTime.Value, isRemoveSecond);
        }
        #endregion

        #region 获取格式化字符串，格式："周一,周二.."
        /// <summary>
        /// 获取格式化字符串，格式："周一,周二.."
        /// </summary>
        /// <param name="dateTime">日期</param>
        public static string ToChineseWeekString(this DateTime dateTime)
        {
            string result = null;
            switch (dateTime.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    result = "周日";
                    break;
                case DayOfWeek.Monday:
                    result = "周一";
                    break;
                case DayOfWeek.Tuesday:
                    result = "周二";
                    break;
                case DayOfWeek.Wednesday:
                    result = "周三";
                    break;
                case DayOfWeek.Thursday:
                    result = "周四";
                    break;
                case DayOfWeek.Friday:
                    result = "周五";
                    break;
                case DayOfWeek.Saturday:
                    result = "周六";
                    break;
                default:
                    break;
            }
            return result;
        }
        #endregion
    }
}
